package com.yupi.springbootinit.bizmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 用于创建测试程序用到的交换机和队列,只执行一次
 */
public class MqInitMain {
    private static final String HOST = "8.134.90.221";
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "123456";
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(HOST);
        connectionFactory.setUsername(USERNAME);
        connectionFactory.setPassword(PASSWORD);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        //创建交换机
        String EXCHANGE_NAME = "code_exchange";
        channel.exchangeDeclare(EXCHANGE_NAME,"direct");
        // 创建队列,随机分配一个队列名称
        String queueName = "code_queue";
        channel.queueDeclare(queueName,true,false,false,null);
        //将交换机和队列进行绑定
        channel.queueBind(queueName,EXCHANGE_NAME,"my_routingKey");
    }
}
